package org.jdesktop.swingx.table.editor;

import org.swing.JExtNumberTextEditor;

import javax.swing.*;
import java.math.BigDecimal;

/**
 * Created by babitsky.viacheslav
 * Date: 03.03.2008
 * Time: 18:36:12
 */
public class NumberCellEditor extends DefaultCellEditor {
    private static final long serialVersionUID = -6832413794551409389L;

    public NumberCellEditor(ValueType valueType, boolean isAllowNegatiate, int presition) {
        this(new JExtNumberTextEditor(), valueType, isAllowNegatiate, presition, null);
    }

    public NumberCellEditor(ValueType valueType, boolean isAllowNegatiate, int presition, int maxChars) {
        this(new JExtNumberTextEditor(), valueType, isAllowNegatiate, presition, maxChars);
    }

    /**
     * Constructs a <code>DefaultCellEditor</code> that uses a text field.
     *
     * @param textEditor a <code>JTextField</code> object
     */
    private NumberCellEditor(final JExtNumberTextEditor textEditor, final ValueType valueType, boolean isAllowNegatiate, int presition, Integer maxChars) {
        super(textEditor);
        textEditor.removeActionListener(delegate);
        textEditor.setAllowNegative(isAllowNegatiate);
        if (maxChars != null) textEditor.setMaxLength(maxChars);
        assert (presition > 0 && valueType == ValueType.BigDecimal) ||
                (presition == 0 && (valueType == ValueType.Integer || valueType == ValueType.Short));
        if (presition > 0) textEditor.setFormat(JExtNumberTextEditor.NumberType.FLOAT);
        else textEditor.setFormat(JExtNumberTextEditor.NumberType.INTEGER);
        textEditor.setPrecision(presition);
        delegate = new EditorDelegate() {
            private static final long serialVersionUID = 3798579654115235414L;

            /**
             * Returns the value of this cell.
             *
             * @return the value of this cell
             */
            @Override
            public Object getCellEditorValue() {
                switch (valueType) {
                    case Integer:
                        return textEditor.getInteger();
                    case Short:
                        return textEditor.getShort();
                }
                return textEditor.getValue();
            }

            /**
             * Sets the value of this cell.
             *
             * @param value the new value of this cell
             */
            @Override
            public void setValue(Object value) {
                switch (valueType) {
                    case Integer:
                        textEditor.setValue((Integer) value);
                        return;
                    case Short:
                        textEditor.setValue((Short) value);
                        return;
                }
                textEditor.setValue((BigDecimal) value);
                final int index = textEditor.getText().indexOf('.');
                if (index > -1){
                    textEditor.setCaretPosition(index);    
                }
            }
        };
        textEditor.addActionListener(delegate);
    }

    public enum ValueType {
        BigDecimal,
        Short,
        Integer
    }
}
